<template>
  <div class="container">
    <el-card class="container-result">
      <thy-caption title="查询结果" margin>
        <el-button
          type="primary clerafix"
          @click="dialogFormVisible = true"
          style="float: right;"
          size="mini"
        >
          导入卡号
        </el-button>
      </thy-caption>
      <el-table size="small" stripe border :data="list" v-loading="loading">
        <el-table-column prop="id" label="序号" width="60" align="center" />
        <el-table-column prop="card" label="卡号" width="120" />
        <el-table-column prop="order_id" label="订单ID" />
        <el-table-column prop="user_id" label="用户ID" />
        <el-table-column prop="statusText" label="状态" />
        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status" style="color: green;">
              已使用
            </span>
            <el-button
              v-else
              type="text"
              size="small"
              @click="deleteHandle(scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        :current-page="page"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="导入卡号" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="卡号" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            autosize
            placeholder="请输入卡号，并用逗号‘,’分割"
            v-model="form.cards"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="uploadCards">导入</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'computer',
  data() {
    return {
      loading: true,
      list: [],
      total: 0,
      page: 1,
      formLabelWidth: '120px',
      form: { cards: '' },
      dialogFormVisible: false
    };
  },
  methods: {
    gotList(page) {
      this.loading = true;
      this.$request
        .get('/admin/card/list?page=' + page + '&pageSize=10')
        .then(data => {
          this.list = data.rows.map(cardinfo => {
            cardinfo.statusText = cardinfo.status == 1 ? '已使用' : '未使用';
            return cardinfo;
          });
          this.total = data.count;
          this.page = page;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleCurrentChange(page) {
      this.gotList(page);
    },
    uploadCards() {
      if (this.cards !== '') {
        this.$request
          .post('/admin/card/add', {
            cards: this.form.cards
          })
          .then(() => {
            this.$message.success('添加成功');
            this.dialogFormVisible = false;
            this.gotList(this.page);
          });
      } else {
        this.$message.error('请输入卡号');
      }
    },
    deleteHandle(idx) {
      this.$confirm('是否继续?', '删除该条目', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$request
            .post('/admin/card/delete', {
              id: this.list[idx].id
            })
            .then(() => {
              this.gotList(this.page);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    }
  },
  mounted() {
    this.gotList(1);
  }
};
</script>

<style >
</style>
